/**
 * Copyright 2008 Frank van Lingen Licensed under the Apache License, Version
 * 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
 * or agreed to in writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */

package superpodder.controller.feedreader;

import java.net.URL;
import java.util.Collections;
import java.util.LinkedList;

import org.gnu.stealthp.rsslib.RSSChannel;
import org.gnu.stealthp.rsslib.RSSHandler;
import org.gnu.stealthp.rsslib.RSSItem;
import org.gnu.stealthp.rsslib.RSSParser;

import superpodder.controller.FeedDownloader;

public class RssLib4J extends FeedReader {

    public RssLib4J(FeedDownloader feedDownloader) {
        super(feedDownloader);
        System.out.println("Creating RssLib4J feedreader");
    }

    public String[] extractPodCasts(String feedUrl, int amount)
            throws Exception {
        System.out.println("Checking url for RssLib4j: " + feedUrl.toString());
        this.formatErrors = 0;
        RSSHandler hand = new RSSHandler();

        try {
            RSSParser.parseXmlFile(feedUrl, hand, false);
        } catch (Exception se1) {
            System.out.println("ERROR Parsing file: " + se1.toString());
            System.out
                    .println("Pretending I did not see this error and continue.");
        }

        LinkedList lst = hand.getRSSChannel().getItems();

        Collections.sort(lst, new RssLib4JComparator());

        int maxFeeds = Math.min(amount, lst.size());
        final String[] feeds = new String[maxFeeds];
        int feedCounter = 0;

        for (int i = 0; i < maxFeeds; i++) {
            RSSItem itm = (RSSItem) lst.get(i);
            String downloadLoc = itm.getEnclosure();
            System.out.println("Retrieved following URL: " + downloadLoc + " "
                    + itm.getDate());

            // check if the url is of the right format. If not increase the
            // error count.
            // if there are too many errors we give up on this feed.
            if (!rightFormat(downloadLoc)) {
                // check if this item has a url. Some items do not have it
                // and we want to skip them.
                System.out.println("format problem");

                if (downloadLoc.compareTo("not_set") == 0) {
                    // increase max feeds
                    this.feedDownloader.log("RSSLib4J " + feedUrl
                            + " URL is not set. Trying to handle\n\n");
                    maxFeeds = Math.min(maxFeeds + 1, lst.size());

                } else {
                    this.feedDownloader
                            .log("RSSLib4J "
                                    + feedUrl
                                    + "--->"
                                    + downloadLoc
                                    + " URL not right format or not encoded in right field \n\n");
                    maxFeeds = Math.min(maxFeeds + 1, lst.size());
                }
            } else {

                feeds[feedCounter] = downloadLoc;
                feedCounter += 1;
            }
            this.reachedMaxErrors();
        }

        return feeds;
    }
}
